
/**
 * @file main
 *
 */

/*********************
 *      INCLUDES
 *********************/
#ifndef _DEFAULT_SOURCE
  #define _DEFAULT_SOURCE /* needed for usleep() */
#endif

#include <stdlib.h>
#include <stdio.h>
#ifdef _MSC_VER
  #include <Windows.h>
#else
  #include <unistd.h>
  #include <pthread.h>
#endif
#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/demos/lv_demos.h"
#include <SDL.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_display_t * hal_init(int32_t w, int32_t h);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

extern void freertos_main(void);

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *      VARIABLES
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

int main(int argc, char **argv)
{
  (void)argc; /*Unused*/
  (void)argv; /*Unused*/

  /*Initialize LVGL*/
  lv_init();

  /*Initialize the HAL (display, input devices, tick) for LVGL*/
  hal_init(840, 480);

  #if LV_USE_OS == LV_OS_NONE

lv_obj_t * ui_SelftestHotfan = lv_obj_create(NULL) ;
// 清除对象的某个标志位，使该对象不再具有该特性。
lv_obj_clear_flag(ui_SelftestHotfan,1L << 4) ;

lv_obj_set_style_radius(ui_SelftestHotfan, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(ui_SelftestHotfan, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);

//设置标签
lv_obj_t * ui_SelftestHotfantitle;
ui_SelftestHotfantitle = lv_label_create(ui_SelftestHotfan);
lv_obj_set_width(ui_SelftestHotfantitle, 600);
    lv_obj_set_height(ui_SelftestHotfantitle, 34);
    lv_obj_set_x(ui_SelftestHotfantitle, 0);
    lv_obj_set_y(ui_SelftestHotfantitle, 20);
    lv_obj_set_align(ui_SelftestHotfantitle, LV_ALIGN_TOP_MID);

    //设置文字
      lv_label_set_text(ui_SelftestHotfantitle, "haofengshan");
    lv_obj_set_style_text_color(ui_SelftestHotfantitle, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_SelftestHotfantitle, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_SelftestHotfantitle, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_SelftestHotfantitle, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);

//设置按钮
 lv_obj_t * ui_SelftestHotfanButton1 = lv_btn_create(ui_SelftestHotfan);
    lv_obj_set_width(ui_SelftestHotfanButton1, 146);
    lv_obj_set_height(ui_SelftestHotfanButton1, 60);
    lv_obj_set_x(ui_SelftestHotfanButton1, 0);
    lv_obj_set_y(ui_SelftestHotfanButton1, 400);
    lv_obj_set_align(ui_SelftestHotfanButton1, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_SelftestHotfanButton1, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_clear_flag(ui_SelftestHotfanButton1, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_radius(ui_SelftestHotfanButton1, 6, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_SelftestHotfanButton1, lv_color_hex(0x42BDD8), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_SelftestHotfanButton1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui_SelftestHotfanButton1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_spread(ui_SelftestHotfanButton1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);

    //设置btn1的标签
    lv_obj_t * ui_SelftestHotfanBtnlabel1 = lv_label_create(ui_SelftestHotfanButton1);
    lv_obj_set_width(ui_SelftestHotfanBtnlabel1, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_SelftestHotfanBtnlabel1, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_SelftestHotfanBtnlabel1, LV_ALIGN_CENTER);
    lv_label_set_text(ui_SelftestHotfanBtnlabel1, "fengshan");
    lv_obj_set_style_text_color(ui_SelftestHotfanBtnlabel1, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_SelftestHotfanBtnlabel1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    // 第二个参数是设置字体可以是内置的，也可以是自定义的
    lv_obj_set_style_text_font(ui_SelftestHotfanBtnlabel1, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);

    //第二个按钮
    lv_obj_t * ui_SelftestHotfanButton2 = lv_btn_create(ui_SelftestHotfan);
    lv_obj_set_width(ui_SelftestHotfanButton2, 90);
    lv_obj_set_height(ui_SelftestHotfanButton2, 50);
    lv_obj_set_x(ui_SelftestHotfanButton2, 697);
    lv_obj_set_y(ui_SelftestHotfanButton2, 14);
    lv_obj_add_flag(ui_SelftestHotfanButton2, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_clear_flag(ui_SelftestHotfanButton2, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_radius(ui_SelftestHotfanButton2, 6, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_SelftestHotfanButton2, lv_color_hex(0x28292D), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_SelftestHotfanButton2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui_SelftestHotfanButton2, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_spread(ui_SelftestHotfanButton2, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置第二个按钮的标签及文字
    lv_obj_t *   ui_SelftestHotfanBtnlabel2 = lv_label_create(ui_SelftestHotfanButton2);
    lv_obj_set_width(ui_SelftestHotfanBtnlabel2, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_SelftestHotfanBtnlabel2, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_SelftestHotfanBtnlabel2, LV_ALIGN_CENTER);
    lv_label_set_text(ui_SelftestHotfanBtnlabel2, "风扇");
    lv_obj_set_style_text_color(ui_SelftestHotfanBtnlabel2, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_SelftestHotfanBtnlabel2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_SelftestHotfanBtnlabel2, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);

    //设置控制板
    lv_obj_t * ui_SelftestHotfanPanel1 = lv_obj_create(ui_SelftestHotfan);
    lv_obj_set_width(ui_SelftestHotfanPanel1, 243);
    lv_obj_set_height(ui_SelftestHotfanPanel1, 294);
    lv_obj_set_x(ui_SelftestHotfanPanel1, 20);
    lv_obj_set_y(ui_SelftestHotfanPanel1, 74);
    lv_obj_clear_flag(ui_SelftestHotfanPanel1, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_radius(ui_SelftestHotfanPanel1, 8, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_SelftestHotfanPanel1, lv_color_hex(0x1C1C1D), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_SelftestHotfanPanel1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_color(ui_SelftestHotfanPanel1, lv_color_hex(0x080809), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_main_stop(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_stop(ui_SelftestHotfanPanel1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui_SelftestHotfanPanel1, LV_GRAD_DIR_VER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_SelftestHotfanPanel1, 0, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_t *  ui_SelftestHotfanlabel5 = lv_label_create(ui_SelftestHotfanPanel1);
    lv_obj_set_width(ui_SelftestHotfanlabel5, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_SelftestHotfanlabel5, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_SelftestHotfanlabel5, 0);
    lv_obj_set_y(ui_SelftestHotfanlabel5, 205);
    lv_obj_set_align(ui_SelftestHotfanlabel5, LV_ALIGN_TOP_MID);
    lv_label_set_long_mode(ui_SelftestHotfanlabel5, LV_LABEL_LONG_SCROLL_CIRCULAR);
    memset(buff, 0, sizeof(buff));
    sprintf(buff, "%s%s", ThroatCoolFan[language_index], PositionArray[language_index]);
    lv_label_set_text(ui_SelftestHotfanlabel5, buff);
    lv_label_set_text(ui_SelftestHotfanlabel5, "大");
    lv_obj_set_style_text_color(ui_SelftestHotfanlabel5, lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_SelftestHotfanlabel5, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_SelftestHotfanlabel5, LV_TEXT_ALIGN_AUTO, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_SelftestHotfanlabel5, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);



    /* Run the default demo */
  /* To try a different demo or example, replace this with one of: */
  /* - lv_demo_benchmark(); */
  /* - lv_demo_stress(); */
  /* - lv_example_label_1(); */
  /* - etc. */

  //加载屏幕
  lv_scr_load(ui_SelftestHotfan);


  while(1) {
    /* Periodically call the lv_task handler.
     * It could be done in a timer interrupt or an OS task too.*/
    lv_timer_handler();
#ifdef _MSC_VER
    Sleep(5);
#else
    usleep(5 * 1000);
#endif
  }

  #elif LV_USE_OS == LV_OS_FREERTOS

  /* Run FreeRTOS and create lvgl task */
  freertos_main();

  #endif

  return 0;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/**
 * Initialize the Hardware Abstraction Layer (HAL) for the LVGL graphics
 * library
 */
static lv_display_t * hal_init(int32_t w, int32_t h)
{

  lv_group_set_default(lv_group_create());

  lv_display_t * disp = lv_sdl_window_create(w, h);

  lv_indev_t * mouse = lv_sdl_mouse_create();
  lv_indev_set_group(mouse, lv_group_get_default());
  lv_indev_set_display(mouse, disp);
  lv_display_set_default(disp);

  LV_IMAGE_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
  lv_obj_t * cursor_obj;
  cursor_obj = lv_image_create(lv_screen_active()); /*Create an image object for the cursor */
  lv_image_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
  lv_indev_set_cursor(mouse, cursor_obj);             /*Connect the image  object to the driver*/

  lv_indev_t * mousewheel = lv_sdl_mousewheel_create();
  lv_indev_set_display(mousewheel, disp);
  lv_indev_set_group(mousewheel, lv_group_get_default());

  lv_indev_t * kb = lv_sdl_keyboard_create();
  lv_indev_set_display(kb, disp);
  lv_indev_set_group(kb, lv_group_get_default());

  return disp;
}
